package com.adv.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adv.controller.base.BaseController;
import com.adv.service.LogoService;
import com.adv.vo.LogoForm;

@Controller
public class LogoController extends BaseController {

	@Autowired
	private LogoService service;

	@RequestMapping("/logo/detail")
	public String detail(ModelMap model) {
		LogoForm form = service.getOnlyLogo();
		if (null != form) {
			model.put("logo", form);
		}
		return "logo/detail";
	}

	@RequestMapping("/logo/add")
	public String add(LogoForm form, ModelMap model) {
		if (null != form && null != form.getId()) {
			model.put("logo", service.getLogoByPrimaryId(form.getId()));
		}
		return "logo/modify";
	}

	@ResponseBody
	@RequestMapping("/logo/save")
	public Object save(@Validated LogoForm form, @Valid BindingResult result, ModelMap model) {
		if (result.hasErrors()) {
			// 新建需要校验完整字段
			return convertField2Map(result);
		}
		return service.saveLogo(form);
	}

	@ResponseBody
	@RequestMapping("/logo/enable")
	public Object enable(LogoForm form) {
		if (null != form.getId()) {
			form.setEnable(true);
			return service.editLogo(form);
		}
		return failureMsg();
	}

	@ResponseBody
	@RequestMapping("/logo/disable")
	public Object disable(LogoForm form) {
		if (null != form.getId()) {
			form.setEnable(false);
			return service.editLogo(form);
		}
		return failureMsg();
	}

	@ResponseBody
	@RequestMapping("/logo/delete")
	public Object delete(LogoForm form) {
		if (null != form.getId()) {
			return service.deleteLogo(form);
		}
		return failureMsg();
	}
}
